<?php
	@require('./dbconnect.php');
	header('Content-Type: application/json');
	$text = $_GET['text'];
	$type = $_GET['type'];
	
	if ($type == 'customer') {
		if (isset($_GET['context']) && $_GET['context'] == 'validateInput') {
			$sql = "SELECT * FROM `customers` WHERE `id` = '" . $text . "';";
		} else {
			$sql = "SELECT * FROM `customers` WHERE `name` like '%" . $text . "%' OR `phone` like '" . $text . "%' OR `email` like '" . $text . "%';";
		}
		
	} else if ($type == 'user') {
		$sql = "SELECT * FROM `users` WHERE `name` like '%" . $text . "%' OR `phone` like '" . $text . "%' OR `email` like '" . $text . "%';";
		
	} else if ($type == 'product') {
		if (isset($_GET['context']) && $_GET['context'] == 'productInputOnchange') {
			$sql = "SELECT `products`.`id` as `prod_id`, `products`.`name` as `prod_name`, `products`.`price` as `prod_price`, `products`.`quantity` as `prod_quan`, `categories`.`name` as `category`, `manufacturers`.`name` as `manufacturer` FROM `products`, `categories`, `manufacturers` WHERE `products`.`category` = `categories`.`id` AND `products`.`manufacturer` = `manufacturers`.`id` AND `products`.`id` = '" . $text . "';";
		} else {
			$sql = "SELECT `products`.`id` as `prod_id`, `products`.`name` as `prod_name`, `products`.`price` as `prod_price`, `products`.`quantity` as `prod_quan`, `categories`.`name` as `category`, `manufacturers`.`name` as `manufacturer` FROM `products`, `categories`, `manufacturers` WHERE `products`.`category` = `categories`.`id` AND `products`.`manufacturer` = `manufacturers`.`id` AND `products`.`name` like '%" . $text . "%';";
		}
	
	} else if ($type == 'category') {
		$sql = "SELECT * FROM `categories` WHERE `name` like '%" . $text . "%';";
	
	} else if ($type == 'manufacturer') {
		$sql = "SELECT * FROM `manufacturers` WHERE `name` like '%" . $text . "%';";
	
	} else if ($type == 'order') {
		if (isset($_GET['context']) && ($_GET['context'] == 'bView' || $_GET['context'] == 'bEdit')) {
			$sql = "SELECT `orders_details`.`product` as `prod_id`, `products`.`name` as `prod_name`, `products`.`price` as `prod_price`, `products`.`quantity` as `prod_quan`, `categories`.`name` as `prod_cate`, `manufacturers`.`name` as `prod_manu`, `orders_details`.`quantity` as `order_quan`, `orders_details`.`sale_off` as `sale_off`, TRUNCATE(`orders_details`.`quantity` * `products`.`price` * (1-(`orders_details`.`sale_off`/100)), 0) as `prod_total_price` FROM `orders_details`, `products`, `categories`, `manufacturers` WHERE `orders_details`.`order` = $text AND `products`.`id` = `orders_details`.`product` AND `products`.`category` = `categories`.`id` AND `products`.`manufacturer` = `manufacturers`.`id`;";
		} else {
			
		}
	}
	
	$resArr = array();
	if ($result = mysqli_query(getConnect(), $sql)) 
	{
		while ($row = mysqli_fetch_assoc($result)) {
			
			if ($type == 'customer') {
				$resArr[] = array('id' => $row['id'], 'name' => $row['name'], 'address' => $row['address'], 'email' => $row['email']);
			
			} else if ($type == 'user') {			
				$resArr[] = array('id' => $row['id'], 'name' => $row['name'], 'address' => $row['address'], 'email' => $row['email']);
				
			} else if ($type == 'product') {
				$resArr[] = array('id' => $row['prod_id'], 'name' => $row['prod_name'], 'price' => $row['prod_price'], 'quantity' => $row['prod_quan'], 'category' => $row['category'], 'manufacturer' => $row['manufacturer']);
	
			} else if ($type == 'category') {
				$resArr[] = array('id' => $row['id'], 'name' => $row['name']);
			
			} else if ($type == 'manufacturer') {
				$resArr[] = array('id' => $row['id'], 'name' => $row['name']);
		
			} else if ($type == 'order') {
				$resArr[] = array('id' => $row['prod_id'], 'name' => $row['prod_name'], 'price' => $row['prod_price'], 'prod_quan' => $row['prod_quan'], 'category' => $row['prod_cate'], 'manufacturer' => $row['prod_manu'], 'order_quan' => $row['order_quan'], 'sale_off' => $row['sale_off'], 'order_price' => $row['prod_total_price']);
			}
		}
		echo json_encode(array('status' => 'success', 'suggest' => $resArr));
	}
	else
	{
		echo json_encode(array('status' => 'error', 'message' => mysqli_error($dbc)));
	}
?>